package drds.binlog.parse.mysql.table_meta_data.dao;

import com.google.common.collect.Maps;
import drds.binlog.common.Authors;
import drds.common.Author;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * 数据的存储
 */
@Author(name = Authors.LI_YANG)
@SuppressWarnings("deprecation")
public class MetaSnapshotDAO extends MetaBaseDAO
{

    public Long insert(MetaSnapshotDO snapshotDO)
    {
        //###return (Long) getSqlMapClientTemplate().insert("meta_snapshot.insert", snapshotDO);
        return 0l;
    }

    public Long update(MetaSnapshotDO snapshotDO)
    {
        //###return (Long) getSqlMapClientTemplate().insert("meta_snapshot.update", snapshotDO);
        return 0l;
    }

    public MetaSnapshotDO findByTimestamp(String destination, Long timestamp)
    {
        HashMap params = Maps.newHashMapWithExpectedSize(2);
        params.put("timestamp", timestamp == null ? 0L : timestamp);
        params.put("destination", destination);

        //###return (MetaSnapshotDO) getSqlMapClientTemplate().queryForObject("meta_snapshot.findByTimestamp", params);
        return null;
    }

    public Integer deleteByName(String destination)
    {
        HashMap params = Maps.newHashMapWithExpectedSize(2);
        params.put("destination", destination);
        //###return getSqlMapClientTemplate().delete("meta_snapshot.deleteByName", params);
        return 0;
    }

    /**
     * 删除interval秒之前的数据
     */
    public Integer deleteByGmtModified(int interval)
    {
        HashMap params = Maps.newHashMapWithExpectedSize(2);
        long timestamp = System.currentTimeMillis() - interval * 1000;
        Date date = new Date(timestamp);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        params.put("timestamp", format.format(date));
        //###return getSqlMapClientTemplate().delete("meta_snapshot.deleteByGmtModified", params);
        return 0;
    }

    protected void initDao() throws Exception
    {
        initTable("meta_snapshot");
    }

}
